import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Location } from '@angular/common';

import { BookService } from '../../../services/book.service';

@Component({
  selector: 'app-booklist',
  templateUrl: './booklist.component.html',
  styleUrls: ['./booklist.component.css']
})
export class BooklistComponent implements OnInit {

  public categoryId: string = '';
  public categoryName: string = '';

  public books: Array<any> = [];
  public tempBooks: Array<any> = [];

  constructor(private location: Location, private currentRouter: ActivatedRoute, private router: Router,
    private bookService: BookService
  ) { }

  ngOnInit() {
    this.categoryId = this.currentRouter.snapshot.params['id'];
    this.categoryName = this.currentRouter.snapshot.params['name'];
    // console.log(this.categoryId,this.categoryName);

    this.bookService.getList().subscribe(res => {
      // console.log(res);
      if (res.Code == 100) {
        this.books = res.Data;
        for (let i = 0; i < this.books.length; i++) {
          if (this.books[i].Book.Category.Id == this.categoryId) {
            this.tempBooks.push(this.books[i]);
          }
        }
        // console.log(this.tempBooks);
      }
    });
  }

  public goDetail(book): void {
    this.router.navigate(['./bookdetail/', book.Book.Id]);
  }

  public goBack(): void {
    this.location.back();
  }

}
